/*
 * (c) Copyright 2014 Hewlett-Packard Development Company, L.P. All rights reserved.
 */
package com.hp.snap.gr.cmd;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hp.snap.gr.utils.Constants;
import com.hp.snap.gr.utils.eium.JMXClientUtils;

/**
 * The JMX client implementation. It supports security model of the eIUM. Typically it is used to connect
 * Central-Management-Server of the eIUM.
 * 
 * @author bow
 * @author Yang, Lin
 * @since 3.2
 */
public class JMXCommandLine {
    private static Logger logger = LoggerFactory.getLogger(JMXCommandLine.class);

    /* Arguments. */
    public static final String Space_Escape = "-spaceEscape";
    public static final String JMX_Bean_Name = "-jmxBeanName";
    public static final String JMX_Attribute_Name = "-jmxAttributeName";

    public static void main0(String[] args) {
        try {
            Map<String, String> ARGUMENTS = new HashMap<String, String>();
            for (int ii = 0; ii < args.length; ii++) {
                String argName = args[ii];

                if (ii + 1 >= args.length) {
                    break;
                }
                String argValue = args[ii + 1];

                ARGUMENTS.put(argName, argValue);
            }

            char space = ARGUMENTS.get(Space_Escape).charAt(0);
            for (Entry<String, String> entry : ARGUMENTS.entrySet()) {
                if (Space_Escape.equals(entry.getKey())) {
                    continue;
                }
                entry.setValue(entry.getValue().replace(space, ' '));
            }

            Object value = JMXClientUtils.getAttribute(ARGUMENTS.get(JMX_Bean_Name), ARGUMENTS.get(JMX_Attribute_Name));

            // output
            Constants.CONSOLE.info(value.toString());
        } catch (Exception e) {
            if (logger.isTraceEnabled()) {
                logger.trace(e.getMessage(), e);
            }
            Constants.CONSOLE
                    .info("==========================================================================================");
            Constants.CONSOLE.info("Usage: JMXCLI [-spaceEscape X] <JMX Bean> <JMX Atttibute>");
            Constants.CONSOLE
                    .info("-spaceEscape=X: While Bean name has space, use X instead, will be translated to space.");
            Constants.CONSOLE.info("Examples:");
            Constants.CONSOLE
                    .info("JMXCLI -spaceEscape ^ -jmxBeanName \"ATOM:type=Server,name=ATOM^Server\" -jmxAttributeName Status");
            Constants.CONSOLE
                    .info("JMXCLI -spaceEscape ^ -jmxBeanName \"ATOM:type=Server,name=ATOM^Server\" -jmxAttributeName Status");
            Constants.CONSOLE
                    .info("==========================================================================================");
        }
    }

    public static void main(String[] args) {
        try {
            main0(args);
        } catch (Exception e) {
            if (logger.isTraceEnabled()) {
                logger.trace("Getting atrribute of MBean is failed!", e);
            }
            System.exit(1);
        } finally {
            try {
                JMXClientUtils.destroy();
            } catch (Exception e) {
                if (logger.isTraceEnabled()) {
                    logger.trace("Destroying JMX connection is failed!", e);
                }
            }
        }
    }
}
